/**
 * 色子
 */
class Dice extends egret.DisplayObjectContainer {
    /** 加载色子图片资源 */
    private dice_png: egret.Bitmap = Main.createBitmapByName("dice_0_png");
    /** 记录帧数 */
    private frameNum = 0;
    /** 按钮开关 */
    private cantTap = false;
    /** 动画执行定时器 */
    private timer = new egret.Timer(800, 1);
    /** roll点 */
    private roll = 0;
    /** 加载效果音 */
    private bgm = RES.getRes("dice_mp3");
    /** 记录音效 */
    private channel;

    /** 提示 */
    private tip = Main.createBitmapByName("start_png");

    public constructor() {
        super();
        this.once(egret.Event.ADDED_TO_STAGE, this.init, this);
    }

    private init() {
        /** 绘制色子 */
        this.addChild(this.dice_png);

        /** 绘制提示 */
        this.addChild(this.tip);
        this.tip.y = -100;

        this.dice_png.width = 215;
        this.dice_png.height = 212;
        this.dice_png.anchorOffsetX = this.dice_png.width / 2;
        this.dice_png.anchorOffsetY = this.dice_png.height;
        this.dice_png.x = this.stage.stageWidth / 2;
        this.dice_png.y = this.stage.stageHeight * .9;
        this.dice_png.touchEnabled = true;
        /** 监听点击事件摇色子roll点 */
        this.dice_png.addEventListener(egret.TouchEvent.TOUCH_TAP, this.diceTap, this);
        /** 监听动画执行定时器执行完毕事件 */
        this.timer.addEventListener(egret.TimerEvent.TIMER_COMPLETE, this.timerOver, this);
    }
    /** 点击事件回调 */
    private diceTap() {
        this.tip.visible = false;
        if (this.cantTap) return;
        /** 关闭按钮开关 */
        this.cantTap = true;
        /**bgm */
        this.channel = this.bgm.play(.3, -1);
        this.channel.volume = .3;
        /** 抽取随机数 */
        // this.roll = parseInt(`${Math.random() * 6}`);

        /** 设置roll点机率 */
        const mivaNum = Math.random() * 100;

        switch (true) {
            case 0 < mivaNum && mivaNum < 20:
                this.roll = 1;
                break;
            case 20 < mivaNum && mivaNum < 60:
                this.roll = 2;
                break;
            case 60 < mivaNum && mivaNum < 80:
                this.roll = 3;
                break;
            case 80 < mivaNum && mivaNum < 90:
                this.roll = 4;
                break;
            case 90 < mivaNum && mivaNum < 99:
                this.roll = 5;
                break;
            case 99 < mivaNum && mivaNum < 100:
                this.roll = 6;
                break;
            default:
                this.roll = 1;
        }


        /** 启动定时器 */
        this.timer.start();
        /** 给色子添加帧事件, 开始动画 */
        this.addEventListener(egret.Event.ENTER_FRAME, this.runDice, this);
    }
    /** 开始roll点动画 */
    private runDice() {
        this.frameNum++;
        if (this.frameNum > 26) this.frameNum = 0;
        this.dice_png.texture = RES.getRes(`dice_item_${this.frameNum}_png`)
    }
    /** 动画定时器结束 */
    private timerOver() {
        this.timer.reset();
        /** 打开按钮开关 */
        this.cantTap = false;
        /** 关闭音效 */
        this.channel.stop();
        /** 移除色子帧事件,停止动画 */
        this.removeEventListener(egret.Event.ENTER_FRAME, this.runDice, this);
        /** 显示roll到的点 */
        this.dice_png.texture = RES.getRes(`dice_${this.roll}_png`);
        /** 派发出去roll点 */
        this.dispatchEventWith("get_roll_num", false, this.roll + 1);
    }

}